package servletContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;

public class servletContextExamples extends HttpServlet {
	@Override
	public void init() throws ServletException {
		super.init();
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String encoding = this.getServletConfig().getInitParameter("encoding");
		System.out.println(encoding);


		InputStream stream = this.getServletContext().getResourceAsStream("dbinfo.properties");
		Properties properties = new Properties();
		properties.load(stream);
		String name = properties.getProperty("name");
		String password = properties.getProperty("password");

		//InputStream resourceAsStream = servletContextExamples.class.getClassLoader().getResourceAsStream("dbinfo.properties");
		//System.out.println(resourceAsStream);

		// 如果是在src目录下，就不能使用servletContext来读取了。
		PrintWriter out = resp.getWriter();
		out.println("name = " + name + ";password=" + password);
	}
}
